package com.sjzs.web.listener;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.sjzs.web.common.CommonQueryDao;
import com.sjzs.web.util.CodeListUtil;


/**
 * 
 * @description:编码监听器
 */
@SuppressWarnings("unchecked")
public class CodeListener implements ServletContextListener {
	
	private static final Logger LOGGER = Logger.getLogger(CodeListener.class);

	/**
	 * @param sce ServletContextEvent
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		LOGGER.info("开始清理代码表缓存...");
		CodeListUtil.destroy();
		LOGGER.info("代码表缓存清理完成。");

	}
	/**
	 * @param sce ServletContextEvent
	 */
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext ctx = sce.getServletContext();
		//String realPath = ctx.getRealPath("/");
		initAa10Code();
		
	}
	
	/**
	 * 
	 * @param ctx ServletContext
	 * @param realPath 路径
	 * @throws SQLException 
	 * @description:初始化编码
	 */
	private void initAa10Code()  {
		LOGGER.info("开始初始化编码表AA10数据...");
		List list = null;
		String sql = "SELECT T.AAA100 AAA100,T.AAA102 AAA102,T.AAA103 AAA103 FROM T_AA10 T WHERE 1 = 1 ORDER BY T.AAA100";
		CommonQueryDao commonQueryDao =new  CommonQueryDao();
		try {
			list = commonQueryDao.doCommonQuery(sql);
			CodeListUtil.initCollListCodeListCache(list);
		} catch (SQLException e) {
			LOGGER.info("编码表AA10数据初始化错误...");
			e.printStackTrace();
		}
		//CodeListUtil.initCodeList2Js(list,realPath);
		LOGGER.info("编码表AA10数据初始化完成...");
	}
}
